/* 
 * JavaScript file with Plane Landing System UI Functions
 */
$(document).ready(function(){
    
	/*Loading of backend info on ui*/ 
    //hide add button
	$('#button_add_airport').hide();
	
	//load carousel
	$('.carousel').carousel({
		interval: 2000
	});
	$('#airport_container').hide();
	
	// Loading of airports list
    var airportList = Airport.list();
    
    // filling select list of airports
    for(a in airportList){
		  var airport = airportList[a];
	      $('#airport_list_select .dropdown-menu')
	      .append('<li class="airport-select-item" data-id="'+airport.name+'">'+
	    		  '<a href="#">'+
	    		  airport.name+
	    		  '</a></li>');
	}
    $('.dropdown-toggle').dropdown();
     
    //TODO: FIX-Landing return values
    //Load the first airplane in the arrival line by default
    //ControlTower.add();
    
    /*End Loading of backend info on ui*/
	
    
    /*Buttons Functions*/
    //Add new plane
    $('#button_add').bind('click',function(){
    	ControlTower.add(); //validation to create the plane in the landing arrival line    	
    });
    
    //remove plane from radar
    $('#button_remove').bind('click',function(){
       //validate that the plane can be removed
    	if($("ul#arrivals").children().hasClass('plane')){
    		var id = $("ul#arrivals li.plane").last().attr("id");
    		id= id.split("_").pop();
    		ControlTower.remove(id);
    	}
    });
    
    //land plane
    $('#button_land').bind('click',function(){
    	//validate that the plane can land
    	var land = false;
        if($("ul#arrivals").children().hasClass('plane')){
           $('[id^="track_"]').each(function(){
               if($(this).is(':empty')){
            	   land = true;
            	   ControlTower.land(); //return the plane which can land
                   return false;
               }
           });
           
           if(!land){
               alert('All the Landing Fields are busy.\nPlease wait...'); 
           }
        }
        else{
        	alert("There are no planes in the Radar Arrival Line.");
        }
    });
    
    //park planes
    $('#button_park').bind('click',function(){
       //validate that the plane can be removed
    	var empty = 0;
    	var total = $('.track').length;
    	$('[id^="track_"]').each(function(){
            if(!$(this).is(':empty')){
            	ControlTower.park();
            	return false;
            }
            else{
            	empty++;
            }
    	});
       
    	if(empty==total){
    		alert("All Landing Fields are empty.\nThere are no planes to park.");
    	}
    });
    
    //add airport
    $('#button_add_airport').bind('click',function(){
        Airport.create("My New Airport", 2); 
      //reload the list
    	airportList = Airport.list();
        // filling select list of airports
        for(a in airportList){
    		  var airport = airportList[a];
    	      $('#airport_list_select .dropdown-menu')
    	      .append('<li class="airport-select-item" data-id="'+airport.name+'">'+
    	    		  '<a href="#">'+
    	    		  airport.name+
    	    		  '</a></li>');
    	}
     });
    
    //set the current airport on the GUI
    $('.airport-select-item').on('click',function(){
    	$('#airport_container_loader').hide();
    	$('#airport_container').show();
        airport = Airport.setCurrent($(this).attr('data-id'));
        setCurrentAirportGUI(airport);
     });
    
    /*End Buttons Functions*/
        
    //tracks functionality
    //activateTracks(); //activate drag-drop function
    //plains functions
    //activatePlains(); //activate drag-drop function
});